/**
 * Copyright (C) 2010 David Simonek <dafesimonek@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.dafe.partyband.db.impl.handlers;

import com.db4o.ObjectContainer;
import com.db4o.ext.Status;
import com.db4o.types.Blob;
import java.io.File;
import java.io.IOException;
import org.dafe.partyband.db.api.ISongResource;
import org.dafe.partyband.db.api.handlers.ISongResourceHandler;
import org.openide.util.Exceptions;

/**
 *
 * @author dafe
 */
public class SongResourceHandler implements ISongResourceHandler {

    private ObjectContainer db;

    public SongResourceHandler(ObjectContainer db) {
        this.db = db;
    }

    public void importFromFileToDb(ISongResource songRes) {
        Blob blob = getResBlob(songRes);
        try {
            blob.readFrom(new File(songRes.getFilePath()));
            double status = blob.getStatus();
            System.out.println("Status: " + status);
            while(status > Status.COMPLETED) {
                try {
                    Thread.sleep(50);
                    status = blob.getStatus();
                    System.out.println("Status: " + status);
                } catch (InterruptedException ex) {
                    System.out.println(ex.getMessage());
                }
            }
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
    }

    public void exportFromDbToFile(ISongResource songRes) {
        Blob blob = getResBlob(songRes);
        try {
            blob.writeTo(new File(songRes.getFilePath()));
            double status = blob.getStatus();
            System.out.println("Status: " + status);
            while(status > Status.COMPLETED) {
                try {
                    Thread.sleep(50);
                    status = blob.getStatus();
                    System.out.println("Status: " + status);
                } catch (InterruptedException ex) {
                    System.out.println(ex.getMessage());
                }
            }
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
    }

    public void store(ISongResource songRes) {
        storeWithoutBlob(songRes);
        importFromFileToDb(songRes);
    }

    public void delete(ISongResource songRes) {
        Blob blob = getResBlob(songRes);
        if (blob.getStatus() != Status.UNUSED) {
            try {
                blob.deleteFile();
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
        db.delete(songRes);
    }

    private Blob getResBlob(ISongResource songRes) {
        Blob blob = songRes.getBlob();
        assert blob != null : "Blob is null, SongResource must be stored into Db first";
        return blob;
    }

    public void setObjectContainer(ObjectContainer db) {
        this.db = db;
    }

    public void storeWithoutBlob(ISongResource songRes) {
        db.store(songRes);
    }

}
